package cmp.viewer.controls;

/*******************************************************
 * POGQueryPanel
 */
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

import cmp.viewer.ViewerFrame;

public class AssemblyCountPanel extends JPanel {
	private static final long serialVersionUID = 5011440646805837338L;
	
	private static final String [] ASSEMBLY_COUNT_OPTIONS = { "All Species", "Any Species" };
		
	public AssemblyCountPanel(ViewerFrame parentFrame) {
		theParentFrame = parentFrame;
		
		Color bgColor = theParentFrame.getSettings().getFrameSettings().getBGColor();
		
		btnSelNotEx = new JButton("Check all not excluded");
		btnSelNotEx.setBackground(bgColor);
		btnSelNotEx.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				incList.setSelectedNot(exList.getSelected());
			}
		});
		btnSelAllInc = new JButton("Select All");
		btnSelAllInc.setMinimumSize(btnSelNotEx.getPreferredSize());
		btnSelAllInc.setMaximumSize(btnSelNotEx.getPreferredSize());
		btnSelAllInc.setBackground(bgColor);
		btnSelAllInc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				incList.setAllSelected(true);
			}
		});
		btnSelNoneInc = new JButton("UnSelect All");
		btnSelNoneInc.setMinimumSize(btnSelNotEx.getPreferredSize());
		btnSelNoneInc.setMaximumSize(btnSelNotEx.getPreferredSize());
		btnSelNoneInc.setBackground(bgColor);
		btnSelNoneInc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				incList.setAllSelected(false);
			}
		});

		btnSelNotInc = new JButton("Check all not included");
		btnSelNotInc.setBackground(bgColor);
		btnSelNotInc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				exList.setSelectedNot(incList.getSelected());
			}
		});
		btnSelAllEx = new JButton("Select All");
		btnSelAllEx.setMinimumSize(btnSelNotInc.getPreferredSize());
		btnSelAllEx.setMaximumSize(btnSelNotInc.getPreferredSize());
		btnSelAllEx.setBackground(bgColor);
		btnSelAllEx.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				exList.setAllSelected(true);
			}
		});
		btnSelNoneEx = new JButton("UnSelect All");
		btnSelNoneEx.setMinimumSize(btnSelNotInc.getPreferredSize());
		btnSelNoneEx.setMaximumSize(btnSelNotInc.getPreferredSize());
		btnSelNoneEx.setBackground(bgColor);
		btnSelNoneEx.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				exList.setAllSelected(false);
			}
		});

		txtIncVal = new JTextField(3);
		txtIncVal.setText("1");
		txtIncVal.setMinimumSize(txtIncVal.getPreferredSize());
		txtIncVal.setMaximumSize(txtIncVal.getPreferredSize());
		
		txtExVal = new JTextField(3);
		txtExVal.setText("1");
		txtExVal.setMinimumSize(txtExVal.getPreferredSize());
		txtExVal.setMaximumSize(txtExVal.getPreferredSize());

		cmbInc = new ButtonComboBox();
		cmbInc.setBackground(bgColor);
		for(int x=0; x<ASSEMBLY_COUNT_OPTIONS.length; x++)
			cmbInc.addItem(ASSEMBLY_COUNT_OPTIONS[x]);
		cmbInc.setSelectedIndex(0);

		cmbEx = new ButtonComboBox();
		cmbEx.setBackground(bgColor);
		for(int x=0; x<ASSEMBLY_COUNT_OPTIONS.length; x++)
			cmbEx.addItem(ASSEMBLY_COUNT_OPTIONS[x]);
		cmbEx.setSelectedIndex(0);

		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setAlignmentX(Component.LEFT_ALIGNMENT);
		setBackground(bgColor);

		JPanel rangePanelInc = createPanelPage();		
		rangePanelInc.add(new JLabel("Include Species"));
		rangePanelInc.add(Box.createVerticalStrut(20));
		
		JPanel tempPanel = createPanelLine();
		tempPanel.add(new JLabel("At least "));
		tempPanel.add(txtIncVal);
		tempPanel.add(new JLabel(" sequence "));
		tempPanel.setMaximumSize(tempPanel.getPreferredSize());	
		rangePanelInc.add(tempPanel);
		rangePanelInc.add(Box.createVerticalStrut(5));
		
		tempPanel = createPanelLine();
		tempPanel.add(new JLabel("From "));
		tempPanel.add(cmbInc);
		tempPanel.add(new JLabel(" selected"));
		tempPanel.setMaximumSize(tempPanel.getPreferredSize());		
		rangePanelInc.add(tempPanel);
		
		JPanel rangePanelEx = createPanelPage();
		rangePanelEx.add(new JLabel("Exclude Species"));
		rangePanelEx.add(Box.createVerticalStrut(20));
		
		tempPanel = createPanelLine();
		tempPanel.add(new JLabel("At most "));
		tempPanel.add(txtExVal);
		tempPanel.add(new JLabel(" sequence "));
		tempPanel.setMaximumSize(tempPanel.getPreferredSize());		
		rangePanelEx.add(tempPanel);
		rangePanelEx.add(Box.createVerticalStrut(5));
		
		tempPanel = createPanelLine();
		tempPanel.add(new JLabel("From "));
		tempPanel.add(cmbEx);
		tempPanel.add(new JLabel(" selected"));
		tempPanel.setMaximumSize(tempPanel.getPreferredSize());		
		rangePanelEx.add(tempPanel);
		
		incList = new AssemList(theParentFrame.getAsmList());
		exList = new AssemList(theParentFrame.getAsmList());

		JPanel buttonPanelInc = createPanelPage();
		buttonPanelInc.add(btnSelAllInc);
		buttonPanelInc.add(Box.createVerticalStrut(5));
		buttonPanelInc.add(btnSelNoneInc);
		buttonPanelInc.add(Box.createVerticalStrut(5));
		buttonPanelInc.add(btnSelNotEx);		
		buttonPanelInc.setMaximumSize(buttonPanelInc.getPreferredSize());

		JPanel incRow = createPanelLine();
		incRow.add(rangePanelInc);
		incRow.add(Box.createHorizontalStrut(10));
		incRow.add(incList);
		incRow.add(Box.createHorizontalStrut(10));
		incRow.add(buttonPanelInc);
		
		JPanel buttonPanelEx = createPanelPage();
		buttonPanelEx.add(btnSelAllEx);
		buttonPanelEx.add(Box.createVerticalStrut(5));
		buttonPanelEx.add(btnSelNoneEx);
		buttonPanelEx.add(Box.createVerticalStrut(5));
		buttonPanelEx.add(btnSelNotInc);		
		buttonPanelEx.setMaximumSize(buttonPanelEx.getPreferredSize());

		JPanel exRow = createPanelLine();
		exRow.add(rangePanelEx);
		exRow.add(Box.createHorizontalStrut(10));
		exRow.add(exList);
		exRow.add(Box.createHorizontalStrut(10));
		exRow.add(buttonPanelEx);

		add(incRow);
		add(Box.createVerticalStrut(25));
		add(exRow);
	}
	
	public String [] getIncludedNames() { return incList.getSelectedLabels(); }
	public String [] getExcludedNames() { return exList.getSelectedLabels(); }
	public void setAllSelectedInc(boolean selected) { incList.setAllSelected(selected); }
	public void setAllSelectedEx(boolean selected) { exList.setAllSelected(selected); }
	public void setIncludeLimit(String value) { txtIncVal.setText(value); }
	public void setExcludeLimit(String value) { txtExVal.setText(value); }
	public int getIncludeLimit() {
		try {
			return Integer.parseInt(txtIncVal.getText());
		}
		catch(Exception e) {
			return 0;
		}
	}
	public int getExcludeLimit() {
		try {
			return Integer.parseInt(txtExVal.getText());
		}
		catch(Exception e) {
			return 0;
		}
	}
	public boolean getIncAnd() { return cmbInc.getSelectedIndex() == 0; }
	public boolean getExAnd() { return cmbEx.getSelectedIndex() == 0; }
	
	private AssemList incList = null, exList = null;
	private JButton btnSelAllInc = null, btnSelNoneInc = null, btnSelNotEx = null;
	private JButton btnSelAllEx = null, btnSelNoneEx = null, btnSelNotInc = null;
	private JTextField txtIncVal = null, txtExVal = null;
	private ButtonComboBox cmbInc = null, cmbEx = null;
	private ViewerFrame theParentFrame = null;
	private class AssemList extends JPanel {
		private static final long serialVersionUID = 5586648551611595554L;

		public AssemList(String [] assemblies) {
			setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
			setAlignmentX(Component.LEFT_ALIGNMENT);
			setBackground(theParentFrame.getSettings().getFrameSettings().getBGColor());

			AssemListItem [] theItems = new AssemListItem[assemblies.length];
			for(int x=0; x<theItems.length; x++)
				theItems[x] = new AssemListItem(assemblies[x], false);
			
			theList = new JList(theItems);
			theList.setCellRenderer(new TheListRenderer());
			theList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			
			theList.addMouseListener(new MouseAdapter() 
			{
				public void mouseClicked(MouseEvent e) {
					JList list = (JList) e.getSource();
					
					int index = list.locationToIndex(e.getPoint());
					AssemListItem item = (AssemListItem)list.getModel().getElementAt(index);
					item.setSelected(!item.isSelected());
					list.repaint(list.getCellBounds(index, index));
				}
			});
			sPane = new JScrollPane(theList);
			sPane.setSize(new Dimension(800, 200));
			sPane.setPreferredSize(new Dimension(200, 100));
			add(sPane);
		}
		
		public String [] getSelectedLabels() {
			int numResults = 0;
			for(int x=0; x<theList.getModel().getSize(); x++) {
				if(((AssemListItem)theList.getModel().getElementAt(x)).isSelected()) {
					numResults++;
				}
			}
			
			if(numResults > 0) {
				String [] retVal = new String[numResults];
				int pos = 0;
				for(int x=0; x<theList.getModel().getSize(); x++) {
					if(((AssemListItem)theList.getModel().getElementAt(x)).isSelected()) {
						retVal[pos] = ((AssemListItem)theList.getModel().getElementAt(x)).toString();
						pos++;
					}
				}
				return retVal;
			}
			
			return null;
		}
		
		public void setAllSelected(boolean selected) {
			for(int x=0; x<theList.getModel().getSize(); x++) {
				((AssemListItem)theList.getModel().getElementAt(x)).setSelected(selected);
			}
			theList.repaint(100);
		}
		
		public boolean [] getSelected() {
			boolean [] retVal = new boolean[theList.getModel().getSize()];
			
			for(int x=0; x<retVal.length; x++) {
				retVal[x] = ((AssemListItem)theList.getModel().getElementAt(x)).isSelected();
			}
			
			return retVal;
		}
		
		public void setSelectedNot(boolean [] selections) {
			for(int x=0; x<selections.length; x++) {
				((AssemListItem)theList.getModel().getElementAt(x)).setSelected(!selections[x]);
			}
			theList.repaint(100);
		}
		
		private JList theList = null;
		private JScrollPane sPane = null;
	}
	
	private class AssemListItem {
		public AssemListItem(String label, boolean selected) {
			strLabel = label;
			bIsSelected = selected;
		}
		
		public boolean isSelected() { return bIsSelected; }
		public void setSelected(boolean selected) { bIsSelected = selected; }
		public String toString() { return strLabel; }
		
		private String strLabel = "";
		private boolean bIsSelected = false;
	}
	
	private class TheListRenderer extends JCheckBox implements ListCellRenderer
	{
		private static final long serialVersionUID = -2234133731431277696L;

		public Component getListCellRendererComponent(	JList list, Object value, int index,
														boolean isSelected, boolean hasFocus)
		{
			setEnabled(list.isEnabled());
			setSelected(((AssemListItem)value).isSelected());
			setFont(list.getFont());
			setBackground(list.getBackground());
			setForeground(list.getForeground());
			setText(value.toString());
			setMaximumSize(getPreferredSize());
			return this;
		}
	}
	static private JPanel createPanelLine() {
	    JPanel retVal = new JPanel();         
	    retVal.setBackground(Color.white);
	    retVal.setAlignmentX(Component.LEFT_ALIGNMENT);         
	    retVal.setLayout(new BoxLayout(retVal, BoxLayout.LINE_AXIS));             
	    return retVal;
	}
	static private JPanel createPanelPage() {
	    JPanel retVal = new JPanel();
	    retVal.setBackground(Color.white);
	    retVal.setAlignmentX(Component.LEFT_ALIGNMENT);         
	    retVal.setLayout(new BoxLayout(retVal, BoxLayout.PAGE_AXIS));
	    return retVal;
	}
	static private JButton createButton(String label) {
		JButton retVal = new JButton(label);
		retVal.setAlignmentX(Component.LEFT_ALIGNMENT);
		retVal.setBackground(Color.white);
		retVal.setMaximumSize(retVal.getPreferredSize());
		retVal.setPreferredSize(retVal.getPreferredSize());
		return retVal;
	}
}
